Import agfx

Class TProperties 

	Field items:StringMap<TProperty> = New StringMap<TProperty>
	Field exist:Bool = False
		
	'|--------------------------------------------------------------------------------
	'| 
	'|--------------------------------------------------------------------------------
	Method Has:Bool(name:String)
		Return Self.items.Contains(name)
	End
	
	'|--------------------------------------------------------------------------------
	'| 
	'|--------------------------------------------------------------------------------
	Method Find:TProperty(name:String)
		Return Self.items.Get(name)
	End
	
	'|--------------------------------------------------------------------------------
	'| 
	'|--------------------------------------------------------------------------------
	Method Get:TProperty(name:String)
		If Self.items.Contains(name) Then
			Return Self.items.Get(name)
		End If
		Return null
	End
	
	'|--------------------------------------------------------------------------------
	'| 
	'|--------------------------------------------------------------------------------
	Method Add:Void(prop:TProperty)
		Self.items.Set(prop.name, prop)	
		If DEBUG Then Print("  name: "+prop.name+" value: "+prop)	
	End	

	Method Add:Void(node:XMLElement,node_name:String="property",attr_name:String="name",attr_val:String="value")
		Local prop:XMLElement
		For prop = EachIn node.Children				
				If prop.Name()=node_name Then
					Self.Add(New TProperty(prop.GetAttribute(attr_name),prop.GetAttribute(attr_val)))
				End If
		Next
	End Method

	'|--------------------------------------------------------------------------------
	'| 
	'|--------------------------------------------------------------------------------
	Method SetProperty:Void(_name:String,_val:Int)
		Self.items.Set(_name, New TProperty(_name,_val))
	End

	Method SetProperty:Void(_name:String,_val:Float)
		Self.items.Set(_name, New TProperty(_name,_val))
	End

	Method SetProperty:Void(_name:String,_val:Bool)
		Self.items.Set(_name, New TProperty(_name,_val))
	End

	Method SetProperty:Void(_name:String,_val:String)
		Self.items.Set(_name, New TProperty(_name,_val))
	End

	Method SetProperty:Void(_name:String,_val:TVector2)
		Self.items.Set(_name, New TProperty(_name,_val))
	End

	'|--------------------------------------------------------------------------------
	'| 
	'|--------------------------------------------------------------------------------
	Method ToString:String(sep:String="|",type_sep:String = ":")
	
		Local res:String = ""
		Local prop:TProperty
		
		For prop = eachin self.items.Values()
			Local typ:String = 0
			Select prop.GetType()
				Case 0
					typ = "int"
				Case 1
					typ = "float"
				Case 2
					typ = "bool"
				Case 3
					typ = "str"
				Case 4
					typ = "vector"
					
			End Select
			
			res = res + prop.name + type_sep+typ+"="+ prop.ToString()+ sep
		Next
		
		res = res[..res.Length()-1]
		Return res	
	End Method
	'|--------------------------------------------------------------------------------
	'| 
	'|--------------------------------------------------------------------------------
	Method Delete:Void()
		SaveState("")
	End
	'|--------------------------------------------------------------------------------
	'| 
	'|--------------------------------------------------------------------------------
	Method Save:Void()
		Local data:String = Self.ToString("|",":")
		SaveState(data)
	End Method
	'|--------------------------------------------------------------------------------
	'| 
	'|--------------------------------------------------------------------------------
	Method Load:Void()
	
		Local state_data:String = LoadState()
		Local split_data:String[] = state_data.Split("|")
	
		
		If state_data.Length()=0 Then			
			Self.exist = False
			Return
		End
	
		Self.exist = True
		
		Local var_item:String[2]
		Local var_typ:String[2]
		Local var_raw:String
	
	
	
		For Local i:Int = 0 to split_data.Length()-1
	
			var_item = split_data[i].Split("=")
			var_typ  = var_item[0].Split(":")
			var_raw  = var_item[1]
	
			
			Select var_typ[1]
				Case "int"
					Self.SetProperty(var_typ[0],Int(var_raw))		
				Case "float"
					Self.SetProperty(var_typ[0],Float(var_raw))		
				Case "bool"
					If var_raw="true" Then Self.SetProperty(var_typ[0],true)
					If var_raw="false" Then Self.SetProperty(var_typ[0],false)
				Case "str"
					Self.SetProperty(var_typ[0],var_raw)
				Case "vector"
					Self.SetProperty(var_typ[0],var_raw)
				
			End Select
		Next

	End Method
	
End Class